package com.takeaway.servlet.filter;

import com.takeaway.const_.Const;
import com.takeaway.pojo.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * @author 后台赖骏
 */
@WebFilter(urlPatterns = {"/manage/*","/jsp/ManageCenter.jsp"})
public class ManageFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        User user = (User)req.getSession().getAttribute("user");
        System.out.println("管理员过滤执行");
            if (user==null||user.getRightId()< Const.MANAGER_ID){
                req.getRequestDispatcher("/user/exitLogin").forward(req,resp);
            }
            else {
                filterChain.doFilter(req, resp);
            }
        }


    @Override
    public void destroy() {

    }
}
